package com.storeroompda.entity;

import android.util.Log;
import com.storeroompda.App;
import com.storeroompda.dao.BillCodeInfoDao;
import com.storeroompda.dao.CodeInfoDao;

import java.util.ArrayList;
import java.util.List;

public class BillCodeControl {
    public static String tag="BillCodeControl";

    //增加条码
    public static long insert(CodeInfo entity)
    {
        long res = -1;
        try {
            res = App.daoSession.getCodeInfoDao().insert(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return res;
        }
    }

    //增加单据
    public static long insert(BillCodeInfo entity)
    {
        long res = -1;
        try {
            res = App.daoSession.getBillCodeInfoDao().insert(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return res;
        }
    }

    public static List<CodeInfo> SearchCodeList()
    {
        List<CodeInfo> list=new ArrayList<CodeInfo>();
        try {
           list = App.daoSession.getCodeInfoDao().queryBuilder().orderAsc(CodeInfoDao.Properties.Id).list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static void deleteCodeList(List<CodeInfo> list)
    {
        try {
            App.daoSession.getCodeInfoDao().deleteInTx(list);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //删除物流码
    public static CodeInfo delete(String code,int type)
    {
        CodeInfo codeInfo=null;
        try {
             codeInfo = App.daoSession.getCodeInfoDao().queryBuilder().where
                    (CodeInfoDao.Properties.Code.eq(code) ,CodeInfoDao.Properties.Type.eq(type)).unique();
            App.daoSession.getCodeInfoDao().delete(codeInfo);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            codeInfo=null;
        }
        finally {
            return codeInfo;
        }
    }

    //删除SN码
    public static CodeInfo deletesn(String code,int type)
    {
        CodeInfo codeInfo=null;
        try {
            codeInfo = App.daoSession.getCodeInfoDao().queryBuilder().where
                    (CodeInfoDao.Properties.Codesn.eq(code) ,CodeInfoDao.Properties.Type.eq(type)).unique();
            App.daoSession.getCodeInfoDao().delete(codeInfo);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            codeInfo=null;
        }
        finally {
            return codeInfo;
        }
    }

    //删除物流或者SN码
    public static CodeInfo deletesnORwl(String code,int type)
    {
        CodeInfo codeInfo=null;
        try {
            codeInfo = App.daoSession.getCodeInfoDao().queryBuilder().whereOr(CodeInfoDao.Properties.Code.eq(code),CodeInfoDao.Properties.Codesn.eq(code))
                    .where(CodeInfoDao.Properties.Type.eq(type)).unique();
            App.daoSession.getCodeInfoDao().delete(codeInfo);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            codeInfo=null;
        }
        finally {
            return codeInfo;
        }
    }


    //删除单据
    public static void delete(BillCodeInfo entity)
    {
        try {
            App.daoSession.getBillCodeInfoDao().delete(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //删除单据下条码
    public static void deleteBill_code(BillCodeInfo entity)
    {
        try {
            List<CodeInfo> codeInfo = App.daoSession.getCodeInfoDao().queryBuilder().
                    where(CodeInfoDao.Properties.Type.eq(entity.getType()),CodeInfoDao.Properties.Iid.eq(entity.getIdd()),CodeInfoDao.Properties.Name.eq(entity.getName())).list();
            App.daoSession.getCodeInfoDao().deleteInTx(codeInfo);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static List<BillCodeInfo> search(int type)
    {
        List<BillCodeInfo> list = new ArrayList<BillCodeInfo>();
        try {
             list = App.daoSession.getBillCodeInfoDao().queryBuilder().where(BillCodeInfoDao.Properties.Type.eq(type)).list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static void update(Long id,int number)
    {
        try {
            BillCodeInfo entity1 = App.daoSession.getBillCodeInfoDao().load(id);
            int num = entity1.getNum()+number;
            entity1.setNum(num);
            App.daoSession.getBillCodeInfoDao().update(entity1);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static int updateCode(Long id,String sncode)
    {
        int res=0;
        try {
            CodeInfo entity1 = App.daoSession.getCodeInfoDao().load(id);
            entity1.setCodesn(sncode);
            App.daoSession.getCodeInfoDao().update(entity1);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            res=-1;
        }
        finally {
            return res;
        }
    }

    public static List<CodeInfo> searchCode(int type,String sncode)
    {
        List<CodeInfo> list = new ArrayList<CodeInfo>();
        try {
            list = App.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(type)
            ,CodeInfoDao.Properties.Codesn.eq(sncode)).list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static List<CodeInfo> searchCodewl(int type,String code)
    {
        List<CodeInfo> list = new ArrayList<CodeInfo>();
        try {
            list = App.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(type)
                    ,CodeInfoDao.Properties.Code.eq(code)).list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static void deleteP()
    {
        try {
            App.daoSession.getProductInfoDao().deleteAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void deleteD()
    {
        try {
            App.daoSession.getDealerInfoDao().deleteAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static long insertP(ProductInfo entity)
    {
        long res = -1;
        try {
            res = App.daoSession.getProductInfoDao().insert(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return res;
        }
    }

    public static long insertD(DealerInfo entity)
    {
        long res = -1;
        try {
            res = App.daoSession.getDealerInfoDao().insert(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return res;
        }
    }

    public static List<ProductInfo> searchP()
    {
        List<ProductInfo> list = new ArrayList<ProductInfo>();
        try {
            list = App.daoSession.getProductInfoDao().loadAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static List<DealerInfo> searchD()
    {
        List<DealerInfo> list = new ArrayList<DealerInfo>();
        try {
            list = App.daoSession.getDealerInfoDao().loadAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return list;
        }
    }

    public static List<CodeInfo> searchCodeAll()
    {
        List<CodeInfo> clist = new ArrayList<CodeInfo>();
//        List<BillCodeInfo> blist = new ArrayList<BillCodeInfo>();
        try {
//            blist = App.daoSession.getBillCodeInfoDao().loadAll();
//            for (int i = 0; i <blist.size() ; i++) {
//                List<CodeInfo> templist = App.daoSession.getCodeInfoDao().queryBuilder().where(CodeInfoDao.Properties.Type.eq(blist.get(i).getType())
//                        ,CodeInfoDao.Properties.Iid.eq(blist.get(i).getIdd()),CodeInfoDao.Properties.Date.eq(blist.get(i).getDate())).list();
//                clist.addAll(templist);
//            }
            clist = App.daoSession.getCodeInfoDao().loadAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return clist;
        }
    }

    public static List<BillCodeInfo> searchBillAll()
    {

        List<BillCodeInfo> blist = new ArrayList<BillCodeInfo>();
        try {
            blist = App.daoSession.getBillCodeInfoDao().loadAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            return blist;
        }
    }

    public static void deleteBillAll()
    {
        try {
            App.daoSession.getBillCodeInfoDao().deleteAll();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}


